package com.jinjia.ordersys.controller;

import com.github.pagehelper.*;
import com.jinjia.ordersys.domain.Order;
import com.jinjia.ordersys.dto.OrderDto;
import com.jinjia.ordersys.dto.UpdateOrderStatusDTO;
import com.jinjia.ordersys.service.OrderService;
import com.jinjia.ordersys.util.*;
import com.jinjia.ordersys.vo.*;
import io.swagger.annotations.*;

import java.util.Collections;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 订单-Controller
 * @Author: fenghouzhi
 * @Date: 11:00 AM 2018/10/9
 */
@Slf4j
@RestController
@Api(tags = "订单信息操作")
@RequestMapping("/api")
public class OrderController extends BaseController {

    @Resource
    private OrderService orderService;

    @PostMapping("/order")
    @ApiOperation(value = "小程序-创建订单")
    ResponseVo addOrder(@RequestBody AddOrderVo addOrderVo) {
        if (addOrderVo == null) {
            return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
        } else if (addOrderVo.getAddGoodsVoList() == null
                || addOrderVo.getAddGoodsVoList().size() < 1) {
            return ResponseVo.warn(ConstantUtil.GOODSLIST_ISNULL);
        }

        log.info("AddOrderVo info:  " + addOrderVo);
        addOrderVo.setOrderTime(DateUtil.getDateTime());
        Integer result = orderService.addOrder(addOrderVo);
        if (result == 1) {
            return ResponseVo.ok();
        } else {
            return ResponseVo.error("下单异常");
        }
    }

    @PutMapping("/order")
    @ApiOperation(value = "小程序-编辑订单")
    ResponseVo editOrder(@RequestBody AddOrderVo editOrderVo) {
        if (null == editOrderVo || null == editOrderVo.getId()) {
            return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
        }
        log.info("editOrderVo info:  " + editOrderVo);
        Integer result = orderService.editOrder(editOrderVo);
        if (result == 1) {
            return ResponseVo.ok();
        } else {
            return ResponseVo.error("下单异常");
        }
    }

    @DeleteMapping("/order/{orderId}")
    @ApiOperation(value = "小程序-删除订单")
    ResponseVo deleteOrder(@PathVariable Long orderId) {
        if (orderId == null) {
            return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
        }
        log.info("orderId info:     " + orderId);
        int deleteResult = orderService.deleteOrder(orderId);
        if (deleteResult > 0) {
            return ResponseVo.ok("success", "删除操作成功");
        } else {
            return ResponseVo.warn("删除操作异常");
        }
    }

    @PutMapping("/order/status")
    @ApiOperation(value = "小程序-订单状态变更")
    ResponseVo updateOrderStatus(@RequestBody UpdateOrderStatusDTO updateOrderStatusDTO) {
        if (null == updateOrderStatusDTO || null == updateOrderStatusDTO.getOrderId()
                || null == updateOrderStatusDTO.getOrderStatus() || null == updateOrderStatusDTO
                .getPersonId()) {
            return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
        }
        Order order = new Order();
        order.setId(updateOrderStatusDTO.getOrderId());
        order.setOrderStatus(updateOrderStatusDTO.getOrderStatus());
        int updateResult = orderService.updateOrder(order);
        if (updateResult < 1) {
            return ResponseVo.warn("修改订单状态异常");
        }
        return ResponseVo.ok();
    }

    @PostMapping("/orders")
    @ApiOperation(value = "小程序-订单列表(提供查询)")
    ResponseVo getOrderDtoList(@RequestBody QueryOrderVo queryOrderVo) {
        try {
            if (queryOrderVo.getPageQueryVo() == null) {
                return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
            }

            if (queryOrderVo.getOrderNo() != null && !"".equals(queryOrderVo.getOrderNo())) {
                queryOrderVo.setOrderNo(queryOrderVo.getOrderNo().trim());
            }

            if (queryOrderVo.getCustomerName() != null && !"".equals(queryOrderVo.getCustomerName())) {
                queryOrderVo.setCustomerName(queryOrderVo.getCustomerName().trim());
            }

            if (queryOrderVo.getCustomerPhone() != null && !"".equals(queryOrderVo.getCustomerPhone())) {
                queryOrderVo.setCustomerPhone(queryOrderVo.getCustomerPhone().trim());
            }
            log.info("queryOrderVo info:    " + queryOrderVo);
            PageHelper.startPage(queryOrderVo.getPageQueryVo().getPageNum(),
                    queryOrderVo.getPageQueryVo().getPageSize());
            List<OrderDto> orderDtoList = orderService.getAllOrderDto(queryOrderVo);
            PageInfo<OrderDto> pageInfo = new PageInfo<>(orderDtoList);
            return ResponseVo.ok("success", getDataForPage(pageInfo));
        } catch (Exception e) {
            log.info(e.getMessage(), e);
            return ResponseVo.warn("获取订单列表异常");
        }
    }

    @GetMapping("/order/{orderId}")
    @ApiOperation(value = "小程序-订单详情")
    ResponseVo getOrderDetail(@PathVariable Long orderId) {
        if (orderId == null) {
            return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
        }
        log.info("orderId info:     " + orderId);
        OrderDto orderDto = orderService.getOrderDetail(orderId);
        if (orderDto != null) {
            return ResponseVo.ok("success", orderDto);
        } else {
            return ResponseVo.warn("未找到订单,请检查");
        }
    }

    @GetMapping("/order/print/{orderId}")
    @ApiOperation(value = "小程序-打印订单")
    ResponseVo printOrder(@PathVariable Long orderId) {
        if (orderId == null) {
            return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
        }
        log.info("orderId info:     " + orderId);
        String printResult = orderService.printOrder(orderId);
        log.info("print result info:     " + printResult);
        return ResponseVo.ok();
    }

    @PostMapping("/orders/history")
    @ApiOperation(value = "小程序-历史订单列表(提供查询)")
    ResponseVo getHistoryOrdersByPersonId(@RequestBody QueryHistoryOrderVo queryHistoryOrderVo) {
        try {
            if (null == queryHistoryOrderVo || null == queryHistoryOrderVo.getPersonId()) {
                return ResponseVo.warn(ConstantUtil.PARAM_ISNULL);
            }
            if (queryHistoryOrderVo.getOrderNo() != null && !""
                    .equals(queryHistoryOrderVo.getOrderNo())) {
                queryHistoryOrderVo.setOrderNo(queryHistoryOrderVo.getOrderNo().trim());
            }
            if (queryHistoryOrderVo.getCustomerName() != null && !""
                    .equals(queryHistoryOrderVo.getCustomerName())) {
                queryHistoryOrderVo.setCustomerName(queryHistoryOrderVo.getCustomerName().trim());
            }
            if (queryHistoryOrderVo.getCustomerPhone() != null && !""
                    .equals(queryHistoryOrderVo.getCustomerPhone())) {
                queryHistoryOrderVo.setCustomerPhone(queryHistoryOrderVo.getCustomerPhone().trim());
            }
            log.info("QueryHistoryOrderVo info:     " + queryHistoryOrderVo.toString());
            PageHelper.startPage(queryHistoryOrderVo.getPageQueryVo().getPageNum(),
                    queryHistoryOrderVo.getPageQueryVo().getPageSize());
            List<OrderDto> historyOrderDtoList = orderService.getAllHistoryOrdersDto(queryHistoryOrderVo);
            if (null != historyOrderDtoList && historyOrderDtoList.size() > 0) {
                PageInfo<OrderDto> pageInfo = new PageInfo<>(historyOrderDtoList);
                return ResponseVo.ok("success", getDataForPage(pageInfo));
            } else {
                return ResponseVo.ok("success", Collections.emptyList());
            }
        } catch (Exception e) {
            log.info(e.getMessage(), e);
            return ResponseVo.warn("获取历史订单列表异常");
        }
    }

}